//////////////////////////////////////////////////////////////////////////////////
//
//	Country On Connect
//		- Version 1.3b
//		- 12.17.2006
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
// 	- Shows country from which a player is in when they connect..
//	- Can set cvars to prevent msgs being shown for admins/bots/normal users...
//	- Select from chat, center, or hud style messages...
//	- If using HUD messages, there's cvars to control different settings for them..
//
//	- * Requires GEOIP module *
//
// Credit:
//
//	- santa_sh0t_tupac: fixing my sloppy coding on bot detection
//
// CVAR: 
//
//	amx_country_connect "1" 			//Turn on/off
//	amx_country_connect_users "1"			//Normal Users -> 1=Show / 0=Dont show
//	amx_country_connect_admins "1"			//Admins -> 1=Show / 0=Dont show
//	amx_country_connect_bots "0"			//Bots -> 1=Show / 0=Dont show
//
//	amx_country_connect_default "United States"	//Country of server (for bots)
//
//	amx_country_connect_msg " * $name is playing from: $country"	//Message to print
//									//$name = client's name
//									//$country = client's country
//
//	amx_country_connect_msgmode "1"			//Message Modes:
//								//1 = Chat message
//								//2 = Hud Message
//
//	amx_country_connect_hudpos "1"			//Hud Message Position:
//								//1 = Center (csay)
//								//2 = Left side (tsay)
//								//3 = Bottom middle
//								//4 = Custom position (use hudx & hudy cvars)
//
//	amx_country_connect_hudx "-1.0"			//Hud X position (-1.0=center, 0.00=left, 1.00=right)
//	amx_country_connect_hudy "-1.0"			//Hud Y position (-1.0=center, 0.00=top, 1.00=bottom)
//
//	amx_country_connect_hudred "255"		//Hud message RED value
//	amx_country_connect_hudgreen "255"		//Hud message GREEN value
//	amx_country_connect_hudblue "255"		//Hud message BLUE value
//
// EXTRA:
//
//	Change #define ADMIN to admin level used for amx_country_connect_admins
//
// Changelog:
//
//	- 07.11.2006 Version 1.0
//		Initial Release
//
//	- 08.06.2006 Version 1.1
//		Changed a return
//
//	- 12.09.2006 Version 1.2
//		If country returns "error", it prints as "Unknown"
//
//	- 12.14.2006 Version 1.3
//		Totally reworked the plugin, INCLUDING CVARs (SO TAKE NOTE OF THAT!!!)
//		Fixed 'default' typo in CVAR
//		Added CVAR to control normal users
//		Now 3 different messages (chat, center, and hud)
//		Various CVARs to control hud message color & position
//		Now a CVAR string sets the message to be printed
//
//	- 12.17.2006 Version 1.3b
//		Much better way of detecting bots (thanks santa_sh0t_tupac)
//		Checks to make sure player is actually connected
//
//////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <amxmisc>
#include <geoip>


#define ADMIN ADMIN_IMMUNITY

new p_connect, p_admins, p_bots, p_users, p_default, p_msg, p_msgmode
new p_red, p_green, p_blue, p_position, p_position_x, p_position_y

public plugin_init()
{
	register_plugin("Country On Connect", "1.3b", "diamond-optic")
	register_cvar("amx_country_connect_stats","1.3b",FCVAR_SERVER|FCVAR_SPONLY)
	
	p_connect = register_cvar("amx_country_connect","1")
	
	p_users = register_cvar("amx_country_connect_users","1")
	p_admins = register_cvar("amx_country_connect_admins","1")
	p_bots = register_cvar("amx_country_connect_bots","1")
	
	p_default = register_cvar("amx_country_connect_default","United States")
	
	p_msg = register_cvar("amx_country_connect_msg"," * $name is playing from: $country")
	
	p_msgmode = register_cvar("amx_country_connect_msgmode","1")
	
	p_position = register_cvar("amx_country_connect_hudpos","1")
	p_position_x = register_cvar("amx_country_connect_hudx","-1.0")
	p_position_y = register_cvar("amx_country_connect_hudy","-1.0")
	
	p_red = register_cvar("amx_country_connect_hudred","255")
	p_green = register_cvar("amx_country_connect_hudgreen","255")
	p_blue = register_cvar("amx_country_connect_hudblue","255")
}

public client_putinserver(id)
{
	if(!get_pcvar_num(p_connect) || !is_user_connected(id) || (access(id,ADMIN) && !get_pcvar_num(p_admins)) || (is_user_bot(id) && !get_pcvar_num(p_bots)) || (!is_user_bot(id) && !access(id,ADMIN) && !get_pcvar_num(p_users)))
		return PLUGIN_CONTINUE
	
	new msg[128]
	get_pcvar_string(p_msg,msg,127)
		
	new name[32]
	get_user_name(id,name,31)
	
	replace(msg,127,"$name",name)
	
	new country[46]
	
	if(is_user_bot(id))
		get_pcvar_string(p_default,country,45)
	else	
		{
		new ip[17]
		
		get_user_ip(id,ip,16,1)
		geoip_country(ip,country,45)
		
		if(equali(country,"error"))
			formatex(country,45,"Unknown")
		}

	replace(msg,127,"$country",country)
		
	switch(get_pcvar_num(p_msgmode))
		{
		case 1: client_print(0,print_chat,"%s",msg)
		case 2: {
			new color[3]
			color[0] = get_pcvar_num(p_red)
			color[1] = get_pcvar_num(p_green)
			color[2] = get_pcvar_num(p_blue)
				
			switch(get_pcvar_num(p_position))
				{
				case 1: set_hudmessage(color[0],color[1],color[2], -1.0, 0.25, 0, 4.0, 4.0, 0.5, 0.3, 4)
				case 2: set_hudmessage(color[0],color[1],color[2], 0.05, 0.67, 0, 4.0, 4.0, 0.5, 0.3, 4)
				case 3: set_hudmessage(color[0],color[1],color[2], -1.0, 0.82, 0, 4.0, 4.0, 0.5, 0.3, 4)
				case 4: set_hudmessage(color[0],color[1],color[2], get_pcvar_float(p_position_x), get_pcvar_float(p_position_y), 0, 4.0, 4.0, 0.5, 0.3, 4)
				}
			
			show_hudmessage(0,"%s",msg)
			}
		}
		
	return PLUGIN_CONTINUE
}
